/**
 * @author:cloud.lg
 * @date:2021.7.26
 * @description: 注册全局自定义指令
 */
import Vue from 'Vue'
export default {
    // 权限标识
    'is-iac': {
        bind (el, binding, vnode, oldVnode) {
            let auth = binding.value
            let authOptions = [];
            if (localStorage.getItem('authOptions')) {
                authOptions = JSON.parse(localStorage.getItem('authOptions'))
            }
            if (authOptions.findIndex(function(item) { return item == auth}) == -1) {
                $(el).addClass('el-hide')
            }
        }
    },
    // 隐藏图标
    'icon-hide': {
        bind (el) {
            if ($(el).find('.el-input__suffix')){
                $(el).find('.el-input__suffix').addClass('el_hide')
            }
        }
    },
    // 表单内input回车切换
    'enterToNext': {
        inserted:function(el, binding, vnode, oldVnode){
            let inputs = el.querySelectorAll('input');
            //绑定回写事件
            for( var i = 0 ;i < inputs.length ; i++ ){
              inputs[i].setAttribute("keyFocusIndex",i);
              inputs[i].addEventListener('keyup', (ev) => {      
                if (ev.keyCode === 13) {
                    // 必填项没有选择不跳过
                    if ($(ev.srcElement).parents('.el-form-item').hasClass('is-required') && $(ev.srcElement).val() === ''){
                        return
                    }
                    let targetTo = ev.srcElement.getAttribute('keyFocusTo');
                  if(targetTo){
                    this.$refs[targetTo].$el.focus();
                  }else{
                    var attrIndex = ev.srcElement.getAttribute('keyFocusIndex');          
                    var ctlI = parseInt(attrIndex);
                    let inputIndex = ctlI + 1
                    vnode.child.$children.forEach((element, index) => {
                        if (element.$children) {
                            // 0项表示没有label
                            if (element.$children[0]) {
                                if (element.$children[0].blur) {
                                    element.$children[0].blur()
                                    return
                                }
                            }
                            // 1项表示label对应的input
                            if (element.$children[1]) {
                                if (element.$children[1].blur) {
                                    element.$children[1].blur()
                                    return
                                }
                            }
                        }
                    });
                    // 跳过禁用的input
                    if(ctlI<inputs.length-1){
                        inputs[ctlI+1].focus();
                        for (var index = inputIndex; index< inputs.length; index++) {
                            if (!inputs[index].disabled) {
                                inputs[index].focus();
                                return
                            }
                        }
                    } else {
                        inputIndex = 0
                        for (var index = inputIndex; index< inputs.length; index++) {
                            if (!inputs[index].disabled) {
                                if(inputs[index].bulr){
                                    inputs[index].bulr();
                                }
                                inputs[index].focus();
                                return
                            }
                        }
                    }
                  }  
                }
              });
            }
        }
    }
}